﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UnitySheet
{
    public class SheetManager : MonoBehaviour
    {

        public static SheetManager sheetManager
        {
            get
            {
                if (!_sheetManager)
                {
                    _sheetManager = FindObjectOfType<SheetManager>();
                }
                return _sheetManager;
            }
        }

        static SheetManager _sheetManager;

        public AnchorPoint anchorPointPrefab;
        public Cell cellPrefabs;
        public RectTransform sheetParent;
        [HideInInspector]
        public List<Sheet> sheets = new List<Sheet>();
        [HideInInspector]
        public Sheet selectedSheet;
        public Sheet sheetPrefab;
        public SheetOperator sheetOperator;

        public delegate void SheetManagerHandler(Sheet sheet);
        public event SheetManagerHandler onSelectOneSheet;
        public event SheetManagerHandler onDeselectSheet;
        public event SheetManagerHandler onDestroyOneSheet;

        public SheetUI sheetUI;
        /// <summary>
        /// 默认宽高
        /// </summary>
        public float width, height;

        /// <summary>
        /// 单元格最小长度
        /// </summary>
        public float minLength;

        /// <summary>
        /// 间隔
        /// </summary>
        public float cellSpace;

        /// <summary>
        /// 最大拆分数量
        /// </summary>
        public int maxSpliteNum = 20;

        public void OnSelectSheet(Sheet sheet)
        {
            if (selectedSheet != sheet)
            {
                if (selectedSheet != null)
                {
                    onDeselectSheet(selectedSheet);
                }
                selectedSheet = sheet;
                if (onSelectOneSheet != null)
                {
                    onSelectOneSheet(sheet);
                }
            }
        }
        /// <summary>
        /// 当选择表格
        /// </summary>
        /// <param name="sheet"></param>
        public void OnDeselectOneSheet(Sheet sheet)
        {
            selectedSheet = null;
            if (onDeselectSheet != null)
            {
                onDeselectSheet(sheet);
            }
        }
        /// <summary>
        /// 当删除表格
        /// </summary>
        /// <param name="sheet"></param>
        public void OnDestroyOneSheet(Sheet sheet)
        {
            if (onDestroyOneSheet != null)
            {
                onDestroyOneSheet(sheet);
            }
            if (selectedSheet == sheet)
            {
                OnDeselectOneSheet(sheet);
            }
            sheets.Remove(sheet);
        }
        /// <summary>
        /// 创建一行一列的初始表格
        /// </summary>
        /// <returns></returns>
        public Sheet CreateSheet()
        {
            Sheet newSheet = Instantiate(sheetPrefab, sheetParent);
            newSheet.CreateNewSheet();
            sheets.Add(newSheet);
            return newSheet;
        }
        /// <summary>
        /// 根据列和行的数量创建表格
        /// </summary>
        /// <param name="columnNum">列数</param>
        /// <param name="RowNum">行数</param>
        /// <returns></returns>
        public Sheet CreateSheet(int columnNum,int RowNum)
        {
            Sheet newSheet = Instantiate(sheetPrefab, sheetParent);
            newSheet.CreateNewSheet();
            for (int c = 1; c < columnNum; c++)
            {
                newSheet.AddColumn();
            }
            for (int r = 1; r < RowNum; r++)
            {
                newSheet.AddRow();
            }
            return newSheet;
        }
        /// <summary>
        /// 根据表格数据创建表格
        /// </summary>
        /// <param name="anchorPoints">所有锚点</param>
        /// <param name="cellPoints">所有单元格的锚点索引</param>
        /// <returns></returns>
        public Sheet CreateSheet(List<List<Vector3>> anchorPoints, int[] cellPoints)
        {
            Sheet newSheet = Instantiate(sheetPrefab, sheetParent);
            newSheet.SetSheet(anchorPoints, cellPoints);
            sheets.Add(newSheet);
            return newSheet;
        }

        public void DeleteSheet()
        {
            selectedSheet.DestroySheet();
        }

        public static void SheetMessage(string msg)
        {
            Debug.Log(msg);
        }

    }
}
